﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 个人收藏 数据访问类Personal_FavorsInfo。
    /// </summary>
    public class Personal_FavorsInfo
    {
        public Personal_FavorsInfo()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Personal_FavorsInfo where RecordId=@RecordId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Entity.Personal_FavorsInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Personal_FavorsInfo(");
            strSql.Append("RecordId,FavorId,FavorType,FavorName,FavorPersonId,FavorPerson,AddTime)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@FavorId,@FavorType,@FavorName,@FavorPersonId,@FavorPerson,@AddTime)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "FavorId", DbType.Guid, model.FavorId);
            db.AddInParameter(dbCommand, "FavorType", DbType.Int32, model.FavorType);
            db.AddInParameter(dbCommand, "FavorName", DbType.AnsiString, model.FavorName);
            db.AddInParameter(dbCommand, "FavorPersonId", DbType.Guid, model.FavorPersonId);
            db.AddInParameter(dbCommand, "FavorPerson", DbType.AnsiString, model.FavorPerson);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Entity.Personal_FavorsInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Personal_FavorsInfo set ");
            strSql.Append("FavorId=@FavorId,");
            strSql.Append("FavorType=@FavorType,");
            strSql.Append("FavorName=@FavorName,");
            strSql.Append("FavorPersonId=@FavorPersonId,");
            strSql.Append("FavorPerson=@FavorPerson,");
            strSql.Append("AddTime=@AddTime");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "FavorId", DbType.Guid, model.FavorId);
            db.AddInParameter(dbCommand, "FavorType", DbType.Int32, model.FavorType);
            db.AddInParameter(dbCommand, "FavorName", DbType.AnsiString, model.FavorName);
            db.AddInParameter(dbCommand, "FavorPersonId", DbType.Guid, model.FavorPersonId);
            db.AddInParameter(dbCommand, "FavorPerson", DbType.AnsiString, model.FavorPerson);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Personal_FavorsInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Entity.Personal_FavorsInfo GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,FavorId,FavorType,FavorName,FavorPersonId,FavorPerson,AddTime from Personal_FavorsInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Entity.Personal_FavorsInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }

                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 4 RecordId,FavorId,FavorType,FavorName,FavorPersonId,FavorPerson,AddTime ");
            strSql.Append(" FROM Personal_FavorsInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "Personal_FavorsInfo");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Entity.Personal_FavorsInfo> GetListArray(string strWhere, string strOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,FavorId,FavorType,FavorName,FavorPersonId,FavorPerson,AddTime ");
            strSql.Append(" FROM Personal_FavorsInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            if (strOrder.Trim() != "")
            {
                strSql.Append(" Order By " + strOrder);
            }

            List<Entity.Personal_FavorsInfo> list = new List<Entity.Personal_FavorsInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }

                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Entity.Personal_FavorsInfo ReaderBind(IDataReader dataReader)
        {
            Entity.Personal_FavorsInfo model = new Entity.Personal_FavorsInfo();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["FavorId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.FavorId = new Guid(ojb.ToString());
            }
            ojb = dataReader["FavorType"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.FavorType = (int)ojb;
            }
            model.FavorName = dataReader["FavorName"].ToString();
            ojb = dataReader["FavorPersonId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.FavorPersonId = new Guid(ojb.ToString());
            }
            model.FavorPerson = dataReader["FavorPerson"].ToString();
            ojb = dataReader["AddTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddTime = (DateTime)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}
